<!--
 * @Descripttion: 虚拟号码
 * @Author: xiao li
 * @Date: 2020-07-06 12:17:07
 * @LastEditors: xiao li
 * @LastEditTime: 2023-01-04 11:55:18
-->
<template>
  <div class="lb-group-news">
    <top-nav></top-nav>
    <div class="page-main">
      <lb-tips>
        <a
          href="https://www.kancloud.cn/nora_123/shangmenyuyue/3100499"
          target="_blank"
          class="c-paragraph"
          >点击查看虚拟号码配置文档</a
        ></lb-tips
      >
      <el-form
        @submit.native.prevent
        :model="subForm"
        :rules="subFormRules"
        ref="subForm"
        label-width="160px"
      >
        <el-form-item label="是否开启虚拟号码" prop="virtual_status">
          <el-radio-group v-model="subForm.virtual_status">
            <el-radio :label="1">{{ $t('action.ON') }}</el-radio>
            <el-radio :label="0">{{ $t('action.OFF') }}</el-radio>
          </el-radio-group>
          <lb-tool-tips
            >开启后，必须配置号码池,
            用于手机端；订单信息里面的电话仅显示部分且拨打电话时显示虚拟号码</lb-tool-tips
          >
        </el-form-item>
        <el-form-item
          label="号码池"
          prop="pool_key"
          v-if="subForm.virtual_status === 1"
        >
          <el-input
            v-model="subForm.pool_key"
            placeholder="请输入号码池"
          ></el-input>
          <lb-tool-tips>号码池, 请前往阿里云获取配置 </lb-tool-tips>
        </el-form-item>
        <el-form-item>
          <lb-button type="primary" @click="submitFormInfo('subForm')">{{
            $t('action.submit')
          }}</lb-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    let checkItem = (rule, value, callback) => {
      if (this.subForm.virtual_status === 1 && !this.subForm.pool_key) {
        callback(new Error('请输入号码池'))
      } else {
        callback()
      }
    }
    return {
      subForm: {
        virtual_status: 0,
        pool_key: ''
      },
      subFormRules: {
        virtual_status: { required: true, type: 'number', message: '请选择是否开启虚拟号码', trigger: 'blur' },
        pool_key: { required: true, validator: checkItem, trigger: 'blur' }
      }
    }
  },
  async created () {
    await this.getDetail()
  },
  methods: {
    async getDetail () {
      let { code, data } = await this.$api.system.virtualConfigInfo()
      if (code !== 200) return
      for (let key in this.subForm) {
        this.subForm[key] = data[key]
      }
    },
    submitFormInfo (name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          let subForm = JSON.parse(JSON.stringify(this.subForm))
          this.$api.system.virtualConfigUpdate(subForm).then(res => {
            if (res.code === 200) {
              this.$message.success(this.$t('tips.successSub'))
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-group-news {
  width: 100%;
  .page-main {
    .el-input {
      width: 300px;
    }
  }
}
</style>
